package cn.touna.jss.modules.custom.batch;

import cn.touna.jss.common.util.Common;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONPath;

import java.io.IOException;
import java.util.Map;

public class RequestThread implements Runnable {

    private RequestParamProcessor paramProcessor;
    private ItemProcessor requestProcessor;
    private JSONObject row;
    private CsvWriter csvWriter;
    private Map<String,String> filedMap;
    private BatchCounter counter;

    public RequestThread(RequestParamProcessor paramProcessor, ItemProcessor requestProcessor, CsvWriter csvWriter,BatchCounter counter) {
        this.paramProcessor = paramProcessor;
        this.requestProcessor = requestProcessor;
        this.csvWriter = csvWriter;
        filedMap = csvWriter.getConfig().getFieldsMap();
        this.counter = counter;
    }

    public void setRow(JSONObject row) {
        this.row = row;
    }

    @Override
    public void run() {
        JSONObject params = paramProcessor.process(row);
        JSONObject result = requestProcessor.process(params);
        if(result != null){
            Object dataObj = result.get("data");
            if(dataObj instanceof String){
                String dataStr = String.valueOf(dataObj);
                try{
                    result.put("data",JSONObject.parseObject(dataStr));
                }catch (Exception e){
                    if(dataStr.startsWith("{") && dataStr.endsWith("}")){
                        result.put("data", Common.mapStr2Json(dataStr));
                    }else{
                        e.printStackTrace();
                    }
                }
            }
        }

        JSONObject out = new JSONObject();
        for (Map.Entry<String, String> entry : filedMap.entrySet()) {
            String path = entry.getValue();
            Object value = getValue(result,path);
            if(value == null){
                value = getValue(params,path);
            }
            if(value == null){
                value = getValue(row,path);
            }
            if(BatchConstant.COLUMN_RESPONSE.equals(path)){
                value = result;
            }else if(BatchConstant.COLUMN_PARAMS.equals(path)){
                value = params;
            }else if(BatchConstant.COLUMN_REQUEST.equals(path)){
                value = row;
            }
            out.put(entry.getKey(),value);
        }
        System.out.println(out.toString());
        try {
            csvWriter.write(out);
        } catch (IOException e) {
            e.printStackTrace();
        }
        counter.incrementWriteCount();
        if(counter.checkWriteComplete()){
            csvWriter.close();
            counter.setWriteComplete(true);
        }
    }



    public Object getValue(JSONObject obj,String columnName){
        if(obj == null)
            return null;
        if(columnName.startsWith("$.")){
            try{
                return JSONPath.eval(obj,columnName);
            }catch (Exception e){
                return null;
            }
        }
        return obj.get(columnName);
    }
}
